﻿@model TBlog.Web.Common.PageHelper.PagedList<TBlog.Web.Models.Article>
@{
    ViewBag.Title = "文章列表";
    Layout = "~/Views/Shared/_ManageLayoutPage.cshtml";


    var categories = ViewBag.Categories as IList<TBlog.Web.Models.Category>;
}

<blockquote class="layui-elem-quote">
    <a href="@Url.Action("Create", "Article")" class="layui-btn"><i class="layui-icon">&#xe61f;</i> 创建文章</a>
</blockquote>
<link href="~/Content/Global.css" rel="stylesheet" />
<blockquote class="layui-elem-quote">
    @using (Html.BeginForm("List", "Article", FormMethod.Get, new { @class = "layui-form" }))
    {
        <div class="layui-form-item blog-manage-form-item-search">
            <div class="layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" value="@ViewBag.ArticleTitle" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">分类</label>
                <div class="layui-input-inline">
                    <select name="category" lay-filter="category">
                        <option value="0">全部分类</option>
                        @if (categories != null && categories.Count > 0)
                        {
                            foreach (var item in categories)
                            {
                            <option value="@item.ID">@item.CategoryName</option>
                            }
                        }
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="state" lay-filter="state">

                        @if (true)
                        {
                            string all = "";
                            string normal = "";
                            string draft = "";
                            var state = ViewBag.State;
                            if (state == null)
                            {
                                all = "selected";
                            }
                            if (state == 1)
                            {
                                normal = "selected";
                            }
                            if (state == 2)
                            {
                                draft = "selected";
                            }
                            <option value="0" @all>全部</option>
                            <option value="1" @normal>正常</option>
                            <option value="2" @draft>草稿</option>
                            
                        }
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <button class="layui-btn" lay-submit="" lay-filter="manage-search">
                    <i class="layui-icon">&#xe615;</i> 搜索
                </button>
            </div>
        </div>
    }
</blockquote>
<div>
    <div class="layui-form">
        <table class="layui-table">
            <colgroup>
                <col width="50">
                <col width="150">
                <col width="150">
                <col width="200">
                <col>
            </colgroup>
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                    <th>所属分类</th>
                    <th>标题</th>
                    <th>作者</th>
                    <th>创建时间</th>
                    <th>浏览量</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
            </thead>

            <tbody>

                @if (Model.Items != null && Model.Items.Count > 0)
                {
                    foreach (var item in Model.Items)
                    {
                
                    <tr>
                        <td>
                            <input type="checkbox" name="" id="@item.ID" lay-skin="primary"></td>

                        <td>@item.Category.CategoryName</td>
                        <td>
                            <a href="@Url.Action("Edit", "Article", new { articleId = item.ID })">@item.Title</a>
                        </td>
                        <td>@item.Author</td>
                        <td>@item.CreateTime.ToString("yyyy-MM-dd HH:mm")</td>

                        <td>@item.Hits</td>
                        @{
                        var state = item.State == 1 ? "正常" : "草稿";
                        }
                        <td>@state</td>

                        <td>
                            <a href="@Url.Action("Detail", "Home", new { id = item.ID })" target="_blank">预览</a>
                            <a href="@Url.Action("Edit", "Article", new { articleId = item.ID })">编辑</a>
                            <a href="javascript:;" data-id="@item.ID" data-name="@item.Title" data-opt="del">删除</a>
                        </td>
                    </tr>
                    }


                }
                else
                {
                    <tr>
                        <td colspan="10" style="text-align: center;">暂无数据</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="blog-manage-page">
        <div id="pagetool" style="padding-left: 20px;"></div>
    </div>
</div>
@section scripts{
    <script>
        layui.use(['laypage', 'jquery', 'layer', 'form'], function () {
            var laypage = layui.laypage
            , $ = layui.jquery
            , form = layui.form()
            , layer = layui.layer;
            //laypage
            //laypage({})
       
            laypage({
                cont: 'pagetool'
                ,curr:@Model.CurrentPage
                , pages: @Model.TotalPages //总页数
                , groups: 5 //连续显示分页数
                ,jump:function(obj, first){
                    var curr = obj.curr;
                    if(!first){
                        location.href = '/manage/article_list_'+curr+'_'+@Model.ItemsPerPage;
                    }
                }
            });
            //绑定删除事件
            $('a[data-opt=del]').each(function () {
                $(this).on('click', function () {
                    var $this = $(this);
                    var name = $this.data('name');
                    var msg = '你真的要把  <span style="color:red;">' + name + '</span> 给删除吗？<img src="' + location.origin + '/Lay/images/face/4.gif" alt="[可怜]">'
                    var title = '萌萌的提示<img src="' + location.origin + '/Lay/images/face/7.gif" alt="[害羞]">'
                    layer.confirm(msg, { icon: 3, title: title }, function (index) {
                        //do something
                        layer.msg('你好狠，居然要删除我！！！');
                        layer.close(index);
                    });
                });
            });
            //全选
            form.on('checkbox(allChoose)', function (data) {
                var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
                child.each(function (index, item) {
                    item.checked = data.elem.checked;
                });
                form.render('checkbox');
            });
            var $item = $('.layui-nav > .layui-nav-item');
            $item.removeClass('layui-this').eq(2).addClass('layui-this');

        });

    </script>
}